package cn.piesat.scanning.controller;

import cn.piesat.scanning.business.dq1043.vo.MetaDataInDbVO;
import cn.piesat.scanning.service.AlgorithmExecService;
import cn.piesat.scanning.utils.ResponseWrapperUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.*;


/**
 * 算法（插件）执行控制层接口，提供调用插件的方法
 *
 * @author xhl
 */
@RestController
@RequestMapping("htScanningController")
public class AlgorithmExecController {

    @Autowired
    private AlgorithmExecService algorithmExecService;
    @Autowired
    private KafkaTemplate kafkaTemplate;


    @GetMapping("/scanStart/{taskId}")
    public String exec(@PathVariable("taskId")String taskId) {
        algorithmExecService.init(taskId);
        return ResponseWrapperUtil.executeCustomTips("200","请求成功！");
    }

    @GetMapping("/redo/one/{recordId}")
    public String redo(@PathVariable("recordId") String recordId) {
        algorithmExecService.redo(recordId);
        return ResponseWrapperUtil.executeResultTips(true);
    }

    @PostMapping("/test")
    public String test(@RequestBody MetaDataInDbVO metaDataInDbVO){
        String topic = "NSS-SPS01";
        for(int j=0;j<10000;j++){
            int rs = (int) ((Math.random() * 9 + 1)* Math.pow(9, 8));
            String filePath = metaDataInDbVO.getFilePath();
            metaDataInDbVO.setTaskPlanId(rs+"");
            String[] str = filePath.split("_");
            int n = (int) ((Math.random() * 9 + 1)* Math.pow(9, 7));
            StringBuilder newFilePath = new StringBuilder();
            for(int i=0;i<str.length;i++){
                if(i == 2){
                    newFilePath.append(n+"_");
                }else{
                    newFilePath.append(str[i]+"_");
                }
            }
            newFilePath.substring(0,newFilePath.lastIndexOf("_")-1);
            String f = newFilePath.toString();
            metaDataInDbVO.setFilePath(f);
            JSONObject rss = JSON.parseObject(JSON.toJSONString(metaDataInDbVO));
            String mes = rss.toJSONString();
            kafkaTemplate.send(topic,mes);
        }

        return "";
    }

}
